﻿//Copyright (c) Microsoft Corporation. All rights reserved.

using Microsoft.ApplicationServer.Caching;
using Microsoft.Ted.Wacel.Base;
using Microsoft.Ted.Wacel.Cache;
using Microsoft.Ted.Wacel.TableStorage;
using System;
using System.Collections.Generic;

namespace Microsoft.Ted.Wacel
{
    public class CacheTableProviderPair<T> : IDataProvider
    {
        WindowsAzureCacheDataProvider mCacheProvider;
        TableStorageDataProvider<T> mTableProvider;
        public CacheTableProviderPair(DataCache cache, string connectionString, string name,
            WindowsAzureCacheConfiguration cacheConfiguration, TableStorageConfiguration tableConfiguration,
            string partitionField = "Key", string rowField = "Key")
        {
            mCacheProvider = new WindowsAzureCacheDataProvider(cache, name) { Configuration = cacheConfiguration };
            mTableProvider = new TableStorageDataProvider<T>(connectionString, name, partitionField, rowField) { Configuration = tableConfiguration };
        }

        public void Put(string key, object value, string partition = null)
        {
            mCacheProvider.Put(key, value, partition);
            mTableProvider.Put(key, value, partition);
        }

        public object Get(string key, string partition = null, Type typeHint = null, Type typeHint2 = null)
        {
            var ret = mCacheProvider.Get(key, partition, typeHint, typeHint2);
            if (ret == null)
                return mTableProvider.Get(key, partition, typeHint, typeHint2);
            else
                return ret;
        }

        public void Update(string key, object value, string partition = null)
        {
            mCacheProvider.Update(key, value, partition);
            mTableProvider.Update(key, value, partition);
        }

        public void Delete(string key, string partition = null)
        {
            mCacheProvider.Delete(key, partition);
            mTableProvider.Delete(key, partition);
        }

        public IEnumerable<KeyValuePair<string, object>> BulkGet(List<string> keys, string partition = null, Type typeHint = null, Type typeHint2 = null)
        {
            var ret = mCacheProvider.BulkGet(keys, partition, typeHint);
            if (ret == null)
                return mTableProvider.BulkGet(keys, partition, typeHint);
            else
                return ret;
        }

        public void BulkPut(IEnumerable<KeyValuePair<string, object>> values, string partition = null)
        {
            mCacheProvider.BulkPut(values, partition);
            mTableProvider.BulkPut(values, partition);
        }

        public void Clear()
        {
            mCacheProvider.Clear();
            mTableProvider.Clear();
        }

        public DataProviderConfiguration Configuration
        {
            get
            {
                return mTableProvider.Configuration;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public IEnumerable<KeyValuePair<string, object>> GetAll(string partition = null, Type typeHint = null, Type typeHint2 = null)
        {
            var ret = mCacheProvider.GetAll(partition, typeHint);
            if (ret == null)
                return mTableProvider.GetAll(partition, typeHint);
            else
                return ret;
        }
        public IDataProvider Clone(string newName)
        {
            throw new NotSupportedException();
        }
    }
}
